package models

import (
	"chinasoccer/pkg/utils"
	"fmt"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// 采购明细号，采购清单号（外码），商品编号（外码），采购数量，商品单价，商品总价，备注。
//  purchasedetailid purchasedetailmasterid purchasedetailgoodsid purchasedetailnum purchasedetailgoodsprice purchasedetailgoodstotle purchasedetailothers

type PurchaseDetail struct {
	gorm.Model
	PurchaseDetailID         string `json:"purchasedetailid" form:"purchasedetailid" gorm:"type:varchar(20); not null;unique;" binding:"required"`
	PurchaseDetailMasterID   string `json:"purchasedetailmasterid" form:"purchasedetailmasterid" gorm:"type:varchar(20); not null;" binding:"required"`
	PurchaseDetailGoodsID    string `json:"purchasedetailgoodsid" form:"purchasedetailgoodsid" gorm:"type:varchar(20); not null;" binding:"required"`
	PurchaseDetailNum        string `json:"purchasedetailnum" form:"purchasedetailnum" gorm:"type:varchar(20); not null;" binding:"required"`
	PurchaseDetailGoodsPrice string `json:"purchasedetailgoodsprice" form:"purchasedetailgoodsprice" gorm:"type:varchar(20); not null;" binding:"required"`
	PurchaseDetailGoodsTotle string `json:"purchasedetailgoodstotle" form:"purchasedetailgoodstotle" gorm:"type:varchar(20); not null;" binding:"required"`
	PurchaseDetailOthers     string `json:"purchasedetailothers" form:"purchasedetailgoodsothers" gorm:"type:varchar(255); not null;" binding:"required"`
	IsDel                    bool   `json:"isdel" form:"isdel" gorm:"type:boolean default false;"`
}

func getJson6(c *gin.Context) PurchaseDetail {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	purchaseDetailID := json["purchasedetailid"]
	purchaseDetailMasterID := json["purchasedetailmasterid"]
	purchaseDetailGoodsID := json["purchasedetailgoodsid"]
	purchaseDetailNum := json["purchasedetailnum"]
	purchaseDetailGoodsPrice := json["purchasedetailgoodsprice"]
	purchaseDetailGoodsTotle := json["purchasedetailgoodstotle"]
	purchaseDetailOthers := json["purchasedetailothers"]
	purchaseDetail := PurchaseDetail{
		PurchaseDetailID:         purchaseDetailID,
		PurchaseDetailMasterID:   purchaseDetailMasterID,
		PurchaseDetailGoodsID:    purchaseDetailGoodsID,
		PurchaseDetailNum:        purchaseDetailNum,
		PurchaseDetailGoodsPrice: purchaseDetailGoodsPrice,
		PurchaseDetailGoodsTotle: purchaseDetailGoodsTotle,
		PurchaseDetailOthers:     purchaseDetailOthers,
	}
	return purchaseDetail
}

// 获取所有的 PurchaseDetails
func GetAllPurchaseDetails(purchaseDetailParam map[string]interface{}) ([]PurchaseDetail, int64, error) {
	var purchaseDetailData []PurchaseDetail
	pagenum := purchaseDetailParam["pagenum"].(int)
	pagesize := purchaseDetailParam["pagesize"].(int)
	query := purchaseDetailParam["query"].(string)
	if query == "" {
		var total int64
		err := db.Table("purchase_details").Where("is_del = false").Count(&total).Error
		if err != nil {
			return purchaseDetailData, 0, err
		}
		err = db.Table("purchase_details").Where("is_del = false").Offset((pagenum - 1) * pagesize).Limit(pagesize).Find(&purchaseDetailData).Error
		fmt.Println("total = ", total)
		return purchaseDetailData, total, err
	}
	purchaseName := query

	var purchase PurchaseDetail
	err := db.Table("purchase_details").Where("purchase_detail_id = ?", purchaseName).First(&purchase).Error
	purchaseDetailData = append(purchaseDetailData, purchase)
	if err != nil {
		return purchaseDetailData, 0, err
	}

	return purchaseDetailData, 1, err
}

// 增添采购明细
func AddPurchaseDetails(c *gin.Context) (PurchaseDetail, int64, error) {
	purchase := getJson6(c)
	var total int64
	db.Table("purchase_details").Where("purchase_detail_id = ?", purchase.PurchaseDetailID).Count(&total)
	if total == 0 {
		err := db.Table("purchase_details").Create(&purchase).Error
		return purchase, total, err
	}
	return purchase, total, nil
}

// 更新采购主表
func UpdatePurchaseDetails(c *gin.Context) (PurchaseDetail, error) {
	purchase := getJson6(c)
	err := db.Table("purchase_details").Where("purchase_detail_id = ?", purchase.PurchaseDetailID).Updates(&purchase).Error
	return purchase, err
}

// 删除采购明细
func DelPurchaseDetails(c *gin.Context) error {
	id := c.Params.ByName("id")
	err := db.Table("purchase_details").Where("purchase_detail_id = ?", id).Update("is_del", true).Error
	if err != nil {
		return err
	}
	err = db.Table("purchase_details").Where("purchase_detail_id = ?", id).Update("purchase_detail_id", utils.RandomString(19)).Error
	return err
}
